package java_thinking.unit_16;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Desc:   搜索数组存储的对象
 * @author: cww
 * @DateTime: 2019/4/10 20:25
 */

public class E24_ArraySearch {
    public static void main(String[] args) {
        Comparator<DataHolder> comparator = new Comparator<DataHolder>() {
            @Override
            public int compare(DataHolder o1, DataHolder o2) {
                return (o1.data < o2.data ? -1 : (o1.data == o2.data ? 0 : 1));
            }
        };

        DataHolder[] a = new DataHolder[10];
        for (int i = 0; i <a.length;i++) {
            a[i] = new DataHolderWithEquals(i);
        }
        Arrays.sort(a,comparator);
        int location = Arrays.binarySearch(a,a[7],comparator);
        System.out.println("Location of " + a[7] + " is " + location);
        if(location >= 0)
            System.out.println(", a[" + location + "] = " + a[location]);
        else
            System.out.println();
        location = Arrays.binarySearch(a, a[5], comparator);
        System.out.println("Location of " + a[5] + " is " + location);
        if(location >= 0)
            System.out.println(", a[" + location + "] = " + a[location]);
    }
}
